package com.zs.grain.ui

import android.app.ProgressDialog
import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.xnkj1688.semplibrary.mvvm.*
import com.zs.grain.*
import com.xnkj1688.semplibrary.recyclerview.*
import com.zs.grain.BR
import com.zs.grain.databinding.FragmentReceipeBinding
import com.zs.grain.tool.XDialog
import com.xnkj1688.semplibrary.dialog.DialogResult

@Route(path = ARouterPath.ReceipeFragment)
class ReceipeFragment : BaseMvvmFragment<FragmentReceipeBinding, ReceipeViewModel>() {

    override val layoutId = R.layout.fragment_receipe

    override fun initViewModel(binding: FragmentReceipeBinding) {
        binding.vm = this.mViewModel
        binding.view = this
    }

    val productItemView = ItemView<ProductItemViewModel>(BR.vm, R.layout.item_product)
    val productClickHandler = ClickHandler { vm: ProductItemViewModel ->
        mViewModel.onClickProduct(vm)
    }

    val selectedBackgroundColor = Color.parseColor("#E0E0E0") //#567845 #8BC34A

    val receipeItemView = ItemView<ReceipeItemViewModel>(BR.vm, R.layout.item_receipe)
    val receipeItemViewColor = ItemView<ReceipeItemViewModel>(BR.vm, R.layout.item_receipecolor)
    val receipeClickHandler = ClickHandler { vm: ReceipeItemViewModel ->
        mViewModel.onClickReceipe(vm)
    }

    override fun onUiNotify(name: String, bundle: Bundle) {
        super.onUiNotify(name, bundle)
        when (name) {
            "AdjustReceipe" -> {
                var id = bundle.getLong("ReceipeId")
                var realMin = 0
                var realMax = 0
                var receipe = mViewModel.getReceipe(id)!!//配方
                var ingrident = mViewModel.getIngrident(receipe.ingridentId.toLong())!!//物料
                if((receipe.min < ingrident.max)&&(receipe.max > ingrident.min)) {
                    if (receipe.min > ingrident.min) realMin = receipe.min
                    else realMin = ingrident.min
                    if (receipe.max > ingrident.max) realMax = ingrident.max
                    else realMax = receipe.max
                }else{
                    realMin = ingrident.min
                    realMax = ingrident.max
                }
                var dlg = AdjustDialog.newInstance(
                    ingrident.name + "(${ingrident.unit})",
                    realMin.toDouble(),
                    realMax.toDouble(),
                    receipe.std.toDouble(),""
                )
                var value = dlg.showDialog(this.activity!!.supportFragmentManager, "AdjustDialog")
                if (value != null) mViewModel.changeValue(id, value.toInt())
            }
            "ShowProgress" -> {
                showProgressDialog("产品测试", "测试中......")
            }
            "HideProgress" -> {
                hideProgressDialog()
            }
            "ShowToast" -> {
                Toast.makeText(context,"请选择产品",Toast.LENGTH_LONG).show()
            }
        }
    }

    fun onClickTestOne(){

        var dlgInput = XDialog(this.activity, R.style.Dialog)
        if (dlgInput.showDialog() !== DialogResult.OK) return
        mViewModel.onClickTest()
    }

    internal var progressingView: ProgressDialog? = null
    // 显示进度
    fun showProgressDialog(title: String, msg: String) {
        if (TextUtils.isEmpty(msg) || TextUtils.isEmpty(title)) {
            return
        }
        if (progressingView == null) {
            progressingView = ProgressDialog(this.activity)//1.创建一个ProgressDialog的实例
        }
        progressingView!!.setTitle(title)//2.设置标题
        progressingView!!.setMessage(msg)
//        progressingView!!.setCancelable(false)//4.设置可否用back键关闭对话框
        progressingView!!.setCanceledOnTouchOutside(false)
        progressingView!!.show()
    }

    fun hideProgressDialog() {
        if (progressingView == null) {
            return
        }
        progressingView!!.hide()
    }
}
